﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Domain.Dto;
using VOL.Domain.Infrastructure;
using VOL.Domain.IServices.EquipmentManagement;
using VOL.Entity.DomainModels;

namespace VOL.Domain.Services.EquipmentManagement
{
    /// <summary>
    /// 点检记录
    /// </summary>
    public class RecordsInspectionsService: IRecordsInspectionsService
    {
        private  readonly IBaseRepository<RecordsInspections> _recordsInspectionsRepository;
        public RecordsInspectionsService(IBaseRepository<RecordsInspections> recordsInspectionsRepository)
        {
            _recordsInspectionsRepository = recordsInspectionsRepository;
        }
        /// <summary>
        /// 点检记录的添加
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> AddRecordsinspections(RecordsInspections recordsInspections)
        {
            var result = await _recordsInspectionsRepository.Add(recordsInspections);
            return result;
        }
        
        /// <summary>
        /// 点检记录的显示
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<RecordsInspections>> GetRecordsinspectionsList(GetRecordsinspectionsListDTO request)
        {
            var RecordsInspectionsList = _recordsInspectionsRepository.GetAll();
            RecordsInspectionsList = RecordsInspectionsList.WhereIF(request.EquipmentCode != null, m => m.EquipmentCode.Contains(request.EquipmentCode));
            RecordsInspectionsList = RecordsInspectionsList.WhereIF(request.EquipmentName != null, m => m.EquipmentName.Contains(request.EquipmentName));
            RecordsInspectionsList = RecordsInspectionsList.WhereIF(request.PlanName != null, m => m.PlanName.Contains(request.PlanName));
            var total = RecordsInspectionsList.Count();
            var items = RecordsInspectionsList.OrderBy(m => m.Id).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            return RecordsInspectionsList.ToList();
        }
        
        /// <summary>
        /// 点检记录的删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> DeleteRecordsinspections(int id)
        {
            var recordsinspections = (await _recordsInspectionsRepository.FindAsync(s => s.Id == id)).FirstOrDefault();
            if (recordsinspections == null) return 0;
            return await _recordsInspectionsRepository.Delete(recordsinspections);
        }

        /// <summary>
        /// 点检记录的修改
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<int> UpdateRecordsinspections(RecordsInspections recordsInspections)
        {
            var result = await _recordsInspectionsRepository.Update(recordsInspections);
            return result;
        }
    }
}
